/*
 * Util.java
 * 
 * Copyright©2012-2013 JMAS corporation. All rights reserved.
 */
package jp.co.jmas.jmasvideo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import jp.co.jmas.jmasvideo.dao.Gender;
import jp.co.jmas.jmasvideo.dao.Student;
import jp.co.jmas.jmasvideo.dao.Video;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.provider.MediaStore.Video.Thumbnails;
import android.util.Log;

/**
 * @author toshio
 *
 */
public class Util {

	/**
	 * @param context
	 * @param filePath
	 * @return
	 */
	public static void createThumbnail(Context context, String videoPath, String thumbnailPath) {
		Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, Thumbnails.MINI_KIND);

		if (bitmap != null) {
			FileOutputStream fout = null;
			try {
				fout = new FileOutputStream(thumbnailPath);
				bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);

			} catch (FileNotFoundException e) {
				Log.e("Util", "Error");
				// do nothing

			} finally {
				if (fout != null) {
					try {
						fout.close();
					} catch (IOException e) {
						// do nothing
					}
				}
			}
		}
	}

	/**
	 * returns 
	 * @param student
	 * @return
	 */
	public static String getStudentLabel(Student student) {
		return String.format("%s組 %s%s",
				student.getClassInfo().getName(),
				student.getKana(),
				student.getGender() == Gender.Boy ? "くん" : "ちゃん");
	}

	/**
	 * returns a file path of video
	 * @param video
	 * @return absolute path
	 */
	public static String getVideoPath(Video video) {
		return new File(AppInfo.getVideoDir(), getVideoName(video)).getAbsolutePath();
	}

	public static String getVideoName(Video video) {
		return video.getKey() + ".mp4";
	}

	/**
	 * returns a file path of thumbnail
	 * @param video
	 * @return absolute path
	 */
	public static String getThumbnailPath(Video video) {
		return new File(AppInfo.getThumbnailDir(), getThumbnailName(video)).getAbsolutePath();
	}

	public static String getThumbnailName(Video video) {
		return video.getKey() + ".jpg";
	}

	public static JSONObject convertVideoToJson(Video video) throws JSONException {
		try {
			JSONObject obj = new JSONObject();
			obj.put("key",    video.getKey());
			obj.put("title", video.getTitle());
			obj.put("desc",  video.getDesc());
			obj.put("size",  video.getSize());
			obj.put("date",  getDateString(video.getDate()));
			obj.put("duration", video.getDuration());
			obj.put("expired",  getDateString(video.getExpired()));
			// TODO 仮対応
			obj.put("email", video.getStudent().getEmail());
			return obj;
		} catch (JSONException e) {
			return null;
		}
	}

	public static Video convertJsonToVideo(JSONObject obj) throws JSONException {
		Video video = new Video();
		video.setKey(obj.getString("key"));
		video.setTitle(obj.getString("title"));
		video.setDesc(obj.getString("desc"));
		video.setSize(obj.getLong("size"));
		video.setDate(parseDate(obj.getString("date")));
		video.setDuration(obj.getLong("duration"));
		video.setExpired(parseDate(obj.getString("expired")));
		return video;
	}

	/**
	 * returns a string from date for JSON
	 * @param date
	 * @return date string
	 */
	private static String getDateString(Date date) {
		return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US).format(date);
	}

	/**
	 * parses a date string in JSON
	 * @param date
	 * @return date object
	 */
	private static Date parseDate(String dateString) {
		try {
			return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US).parse(
				dateString.endsWith("Z") ?
					dateString.substring(0, dateString.length() -1) + "+0000" :
					dateString);

		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

}
